<?php

namespace app\home\controller;

use app\common\controller\Home;

class Coupon extends Home
{
    //不需要登录
    public $NoLogin = ['index', 'info', 'receive'];

    public function __construct()
    {
        parent::__construct();

        $this->CouponModel = model('Coupon.Coupon');
        $this->ReceiveModel = model('Coupon.Receive');
    }

    //列表界面
    public function index()
    {
        //判断是否有登录
        $login = $this->IsLogin(false);

        $list = $this->CouponModel->where(['status' => '1'])->select();

        if($list)
        {
            foreach($list as $item)
            {
                if($login)
                {
                    $where = ['cid' => $item['id'], 'busid' => $login['id']];
                    $receive = $this->ReceiveModel->where($where)->find();
                    $item['receive'] = $receive ? false : true;
                }else
                {
                    //没有登录 默认为可以领取
                    $item['receive'] = true;
                }
            }
        }

        $this->assign('list', $list);

        return $this->view->fetch();
    }

    //详情界面
    public function info()
    {
        $cid = $this->request->param('cid', 0, 'trim');

        //查询优惠券详细信息
        $coupon = $this->CouponModel->find($cid);

        if(!$coupon)
        {
            $this->error('优惠券不存在');
            exit;
        }

        //判断是否有登录
        $login = $this->IsLogin(false);

        if($login)
        {
            $where = ['cid' => $cid, 'busid' => $login['id']];
            $receive = $this->ReceiveModel->where($where)->find();
            $coupon['receive'] = $receive ? false : true;
        }else
        {
            //没有登录 默认为可以领取
            $coupon['receive'] = true;
        }

        $this->assign('coupon', $coupon);

        return $this->view->fetch();
    }

    //领取
    public function receive()
    {
        $cid = $this->request->param('cid', 0, 'trim');

        //查询优惠券详细信息
        $coupon = $this->CouponModel->find($cid);

        if(!$coupon)
        {
            $this->error('优惠券不存在');
            exit;
        }

        //手动判断是否登录
        $this->IsLogin(true, url('home/coupon/index'));

        //优惠券(更新表)/优惠券领取表(插入)

        //开启事务
        $this->CouponModel->startTrans();
        $this->ReceiveModel->startTrans();

        //判断活动是否在进行中
        if($coupon['status'] == '0')
        {
            $this->error('活动已结束');
            exit;
        }

        //查询当前用户是否有领取过该优惠券
        $where = [
            'cid' => $cid,
            'busid' => $this->view->AutoLogin['id']
        ];

        $check = $this->ReceiveModel->where($where)->find();

        if($check)
        {
            $this->error('已领取过该优惠券，不能重复领取');
            exit;
        }

        $total = isset($coupon['total']) ? trim($coupon['total']) : 0;
        $total = intval($total);
        $total = $total < 0 ? 0 : $total;
        $total--;

        //优惠券表的更新
        $CouponData = [
            'id' => $cid,
            'total' => $total
        ];

        $CouponStatus = $this->CouponModel->isUpdate(true)->save($CouponData);

        if($CouponStatus === FALSE)
        {
            $this->error('更新优惠券数量失败');
            exit;
        }

        //优惠券领取表的插入
        $ReceiveData = [
            'cid' => $cid,
            'busid' => $this->view->AutoLogin['id'],
            'status' => '1'
        ];

        $ReceiveStatus = $this->ReceiveModel->validate('common/Coupon/Receive')->save($ReceiveData);

        if($ReceiveStatus === FALSE)
        {
            $this->CouponModel->rollback();
            $this->error($this->ReceiveModel->getError());
            exit;
        }

        //综合判断
        if($CouponStatus === FALSE || $ReceiveStatus === FALSE)
        {
            $this->ReceiveModel->rollback();
            $this->CouponModel->rollback();
            $this->error('领取失败');
            exit;
        }else
        {
            //提交事务
            $this->CouponModel->commit();
            $this->ReceiveModel->commit();
            $this->success('领取成功', url('home/coupon/index'));
            exit;
        }
    }
}
